package com.haocang.filedemo.config.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.sctp.nio.NioSctpServerChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
public class NettyServer {

    private static class SingletionServer {
        static final NettyServer nettyServer = new NettyServer();
    }


    public static NettyServer getInstance() {
        return SingletionServer.nettyServer;
    }

    private EventLoopGroup mainGroup;
    private EventLoopGroup subGroup;
    private ServerBootstrap serverBootstrap;
    private ChannelFuture channelFuture;

    public NettyServer() {
        mainGroup = new NioEventLoopGroup();
        subGroup = new NioEventLoopGroup();
        serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(mainGroup, subGroup)
                .channel(NioSctpServerChannel.class)
                .childHandler(new NettyChannelInitializer());
    }

    public void start() {
        // 绑定端口
        channelFuture = this.serverBootstrap.bind(8081);
        log.info("netty服务端 启动成功");
    }

}
